using System.Collections;
using Unity.VisualScripting;
using UnityEngine;

public class Mucus : MonoBehaviour
{
    public LayerMask targetLayer;    
    public Vector2  direction;
    //[Tooltip("释放飞盘时施加的速度大小")]
    //public float force;
    public float releaseTime = 1.5f;
    Animator animator;
    public AudioClip OnReleaseClip,
        OnReceiveClip;
    AudioSource audioSource;

    private Boomerang boomerang;

    //是否持有飞盘
    bool isAccess = false;

    private void Start()
    {        
        animator = GetComponent<Animator>();
        audioSource = GetComponent<AudioSource>();
    }

    //触发器触发
    public void OnRelease()
    {
        animator.SetTrigger("OnRelease");
        audioSource.PlayOneShot(OnReleaseClip);
        if (!isAccess || boomerang==null)
            return;
        boomerang.Initialize(direction);        

        isAccess = false;
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (!isAccess && (1 << collision.gameObject.layer & targetLayer) != 0 )
        {            
            collision.gameObject.TryGetComponent<Boomerang>(out this.boomerang);
            boomerang.EnterCatchState();
            
            collision.transform.position = transform.position;
            animator.SetTrigger("OnReceive");
            audioSource.PlayOneShot(OnReceiveClip);
            isAccess = true;
        }
    }
}
